/*
 * Created by JFormDesigner on Mon Oct 17 19:23:33 CST 2022
 */

package com.liyu.ui;

import com.liyu.message.Request;
import com.liyu.message.Response;
import com.liyu.message.ResponseStatus;
import com.liyu.pojo.User;
import com.liyu.util.ClientUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.IOException;

/**
 * @author destiny
 */
public class LoginFrame extends JFrame {
    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel LoginPanel;
    private JPanel LeftBoderPanel;
    private JLabel label3;
    private JLabel label5;
    private JPanel InputPanel;
    private JLabel UserNameLabel;
    private JTextField UserNameField;
    private JLabel IntervalLabel;
    private JLabel PasswordLabel;
    private JPasswordField PasswordField;
    private JLabel MessageLable;
    private JPanel RightBoderPanel;
    private JLabel label6;
    private JPanel ComtrolPanel;
    private JButton LoginButton;
    private JButton RedistButton;
    private JButton VisitButton;
    private JPanel HuanyinPanel;
    private JLabel label4;
    private JLabel label1;
    private JLabel label2;

    public LoginFrame() {
        this.initComponents();
        setVisible(true);
    }

    public void disPose() {
        this.dispose();
    }

    private void login(ActionEvent e) {
        // TODO add your code here
        String username = UserNameField.getText();
        String password = new String(PasswordField.getPassword());
        Request request = new Request();
        request.setAction("userLogin");
        request.setAttribute("id", username);
        request.setAttribute("password", password);

        Response response = null;
        try {
            response = ClientUtil.sendTextRequest(request);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        if (response.getStatus() == ResponseStatus.OK) {
            User user = (User) response.getData("user");
            System.out.println(user);
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        LoginPanel = new JPanel();
        LeftBoderPanel = new JPanel();
        label3 = new JLabel();
        label5 = new JLabel();
        InputPanel = new JPanel();
        UserNameLabel = new JLabel();
        UserNameField = new JTextField();
        IntervalLabel = new JLabel();
        PasswordLabel = new JLabel();
        PasswordField = new JPasswordField();
        MessageLable = new JLabel();
        RightBoderPanel = new JPanel();
        label6 = new JLabel();
        ComtrolPanel = new JPanel();
        LoginButton = new JButton();
        RedistButton = new JButton();
        VisitButton = new JButton();
        HuanyinPanel = new JPanel();
        label4 = new JLabel();
        label1 = new JLabel();
        label2 = new JLabel();

        //======== this ========
        setMaximizedBounds(new Rectangle(0, 0, 420, 330));
        setFont(new Font(Font.DIALOG, Font.PLAIN, 13));
        setResizable(false);
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== LoginPanel ========
        {
            LoginPanel.setPreferredSize(new Dimension(0, 0));
            LoginPanel.setLayout(new BorderLayout());

            //======== LeftBoderPanel ========
            {
                LeftBoderPanel.setPreferredSize(new Dimension(50, 0));
                LeftBoderPanel.setLayout(new FlowLayout());
                LeftBoderPanel.add(label3);

                //---- label5 ----
                label5.setPreferredSize(new Dimension(50, 17));
                LeftBoderPanel.add(label5);
            }
            LoginPanel.add(LeftBoderPanel, BorderLayout.WEST);

            //======== InputPanel ========
            {
                InputPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

                //---- UserNameLabel ----
                UserNameLabel.setText("\u8d26\u53f7\uff1a");
                UserNameLabel.setPreferredSize(new Dimension(50, 30));
                InputPanel.add(UserNameLabel);

                //---- UserNameField ----
                UserNameField.setPreferredSize(new Dimension(200, 30));
                InputPanel.add(UserNameField);

                //---- IntervalLabel ----
                IntervalLabel.setPreferredSize(new Dimension(250, 17));
                IntervalLabel.setText("               ");
                InputPanel.add(IntervalLabel);

                //---- PasswordLabel ----
                PasswordLabel.setText("\u5bc6\u7801\uff1a");
                PasswordLabel.setPreferredSize(new Dimension(50, 30));
                InputPanel.add(PasswordLabel);

                //---- PasswordField ----
                PasswordField.setPreferredSize(new Dimension(200, 30));
                InputPanel.add(PasswordField);

                //---- MessageLable ----
                MessageLable.setPreferredSize(new Dimension(250, 17));
                MessageLable.setText("               ");
                InputPanel.add(MessageLable);
            }
            LoginPanel.add(InputPanel, BorderLayout.CENTER);

            //======== RightBoderPanel ========
            {
                RightBoderPanel.setPreferredSize(new Dimension(50, 0));
                RightBoderPanel.setLayout(new FlowLayout());

                //---- label6 ----
                label6.setPreferredSize(new Dimension(50, 17));
                RightBoderPanel.add(label6);
            }
            LoginPanel.add(RightBoderPanel, BorderLayout.EAST);

            //======== ComtrolPanel ========
            {
                ComtrolPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 40, 5));

                //---- LoginButton ----
                LoginButton.setText("\u767b\u5f55");
                LoginButton.addActionListener(e -> login(e));
                ComtrolPanel.add(LoginButton);

                //---- RedistButton ----
                RedistButton.setText("\u6ce8\u518c");
                ComtrolPanel.add(RedistButton);

                //---- VisitButton ----
                VisitButton.setText("\u968f\u4fbf\u770b\u770b");
                ComtrolPanel.add(VisitButton);
            }
            LoginPanel.add(ComtrolPanel, BorderLayout.SOUTH);

            //======== HuanyinPanel ========
            {
                HuanyinPanel.setPreferredSize(new Dimension(34, 100));
                HuanyinPanel.setLayout(new FlowLayout());

                //---- label4 ----
                label4.setPreferredSize(new Dimension(100, 17));
                HuanyinPanel.add(label4);

                //---- label1 ----
                label1.setText("\u9ca4\u9c7c\u6613\u6362");
                label1.setPreferredSize(new Dimension(310, 20));
                HuanyinPanel.add(label1);

                //---- label2 ----
                label2.setText("           \u2014\u2014\u503c\u5f97\u4e0a\u624b\u7684\u6821\u56ed\u4e8c\u624b\u4ea4\u6613\u5e73\u53f0o(= =)m");
                label2.setPreferredSize(new Dimension(310, 20));
                HuanyinPanel.add(label2);
            }
            LoginPanel.add(HuanyinPanel, BorderLayout.NORTH);
        }
        contentPane.add(LoginPanel, BorderLayout.CENTER);
        setSize(400, 300);
        setLocationRelativeTo(null);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
